<template>
  <div class="home">
    <el-card style="margin-bottom: 40px" class="box-card" shadow="hover">
      <div style="display: flex">
        <div style="margin-right: 30px">
          <label class="el-form-item__label">商品名称: </label>
          <el-input
              style="width: 160px"
              placeholder="请输入商品名称"
              v-model="params.title"
          ></el-input>
        </div>
        <div style="margin-right: 30px">
          <label class="el-form-item__label">商品卖点: </label>
          <el-input
              style="width: 160px"
              placeholder="请输入商品卖点"
              v-model="params.sellPoint"
          ></el-input>
        </div>

        <el-button style="margin-left: 5px" type="primary" @click="load">
          <i class="el-icon-search"></i>搜索
        </el-button>
        <el-button style="margin-left: 8px" @click="reset">
          <i class="el-icon-refresh-right"></i>重置
        </el-button>
      </div>
    </el-card>
    <el-card class="box-card" shadow="hover">
      <div style="display: flex; margin-bottom: 10px">
        <span style="font-weight: bold">商品详情</span>

        <el-button
            style="margin-left: auto; margin-right: 40px"
            type="primary"
            @click="add"
        >
          <i class="el-icon-circle-plus-outline"></i>
          添加商品
        </el-button>
      </div>

      <el-table
          :data="tableData"
          stripe
          border
          height="600"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
            prop="id"
            label="商品Id"
        ></el-table-column>
        <el-table-column
            prop="itemType"
            label="商品名称"
        ></el-table-column>
        <el-table-column
            prop="title"
            label="商品标题"
        ></el-table-column>
        <el-table-column
            prop="sellPoint"
            label="商品卖点"
        ></el-table-column>
        <el-table-column
            prop="price"
            label="商品价格"
        ></el-table-column>
        <el-table-column
            prop="num"
            label="商品库存"
        ></el-table-column>

        <el-table-column
            prop="image"
            label="商品图片"
        ></el-table-column>

        <el-table-column
            prop="priority"
            label="商品优先级"
        ></el-table-column>


        <el-table-column
            prop="createdUser"
            label="创建人"
        ></el-table-column>
        <el-table-column
            prop="createdTime"
            label="创建时间"
        >
          <template slot-scope="scope">
            {{ scope.row.createdTime | formatTime }}
          </template>
        </el-table-column>

        <el-table-column
            prop="modifiedUser"
            label="修改人"
        ></el-table-column>
        <el-table-column
            prop="modifiedTime"
            label="修改时间"
        >
          <template slot-scope="scope">
            {{ scope.row.modifiedTime | formatTime }}
          </template>
        </el-table-column>


        <el-table-column
            prop="status"
            label="状态"
        ></el-table-column>

        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <el-button type="text" @click="edit(scope.row)">
              <i class="el-icon-edit"></i>
              编辑商品分类
            </el-button>
            <el-popconfirm
                style="margin-left: 4px"
                title="您确定删除这行数据吗？"
                @confirm="del(scope.row.id)"
            >
              <el-button
                  slot="reference"
                  type="text"
              >
                <i class="el-icon-delete"></i>删除
              </el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--  分页-->
      <div style="margin-top: 20px; display: flex">
        <el-pagination
            background
            :current-page="params.pageNum"
            :page-size="params.pageSize"
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
            :total="total"
        >
        </el-pagination>
      </div>

      <el-dialog
          :visible.sync="IsAdd"
          title="新增商品"
          width="60%"
      >
        <el-form :model="addForm" ref="addform" label-width="80px">
          <el-row>
            <!-- 左侧输入框 -->
            <el-col :span="8">
              <!-- 商品系列 -->
              <el-form-item label="商品名称" prop="itemType">
                <el-input v-model="addForm.itemType"></el-input>
              </el-form-item>
            </el-col>
            <!-- 商品标题 -->
            <el-col :span="8">
              <el-form-item label="商品标题" prop="title">
                <el-input v-model="addForm.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <!-- 商品卖点 -->
              <el-form-item label="商品卖点" prop="sellPoint">
                <el-input v-model="addForm.sellPoint"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 商品分类选择框 -->
          <el-row>


            <!-- 右侧输入框 -->
            <el-col :span="8">
              <!-- 商品价格 -->
              <el-form-item label="商品价格" prop="price">
                <el-input v-model.number="addForm.price"></el-input>
              </el-form-item>

            </el-col>

            <el-col :span="8">
              <!-- 商品库存 -->
              <el-form-item label="商品库存" prop="num">
                <el-input v-model.number="addForm.num"></el-input>
              </el-form-item>

            </el-col>
            <el-col :span="8">
              <el-form-item label="分类id" prop="categoryId">
                <el-input v-model.number="addForm.categoryId"></el-input>
              </el-form-item>
            </el-col>

          </el-row>

          <!-- 商品状态和商品优先级 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="商品状态" prop="status">
                <el-select v-model="addForm.status" placeholder="请选择" style="width: 200px">
                  <el-option label="上架" value="1"></el-option>
                  <el-option label="下架" value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="优先级" prop="priority">
                <el-input v-model.number="addForm.priority"></el-input>
              </el-form-item>
            </el-col>


          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="图片URL" prop="price">
                <el-input v-model.number="addForm.image"></el-input>
              </el-form-item>
            </el-col>
          </el-row>


        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="IsAdd = false">取 消</el-button>
          <el-button type="primary" @click="addMsg">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="商品分类" :visible.sync="IsEdit" width="60%">
        <el-form :model="editForm" ref="editform" label-width="80px">
          <el-row>
            <!-- 左侧输入框 -->
            <el-col :span="8">
              <!-- 商品系列 -->
              <el-form-item label="商品名称" prop="itemType">
                <el-input v-model="editForm.itemType"></el-input>
              </el-form-item>
            </el-col>
            <!-- 商品标题 -->
            <el-col :span="8">
              <el-form-item label="商品标题" prop="title">
                <el-input v-model="editForm.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <!-- 商品卖点 -->
              <el-form-item label="商品卖点" prop="sellPoint">
                <el-input v-model="editForm.sellPoint"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 商品分类选择框 -->
          <el-row>


            <!-- 右侧输入框 -->
            <el-col :span="8">
              <!-- 商品价格 -->
              <el-form-item label="商品价格" prop="price">
                <el-input v-model.number="editForm.price"></el-input>
              </el-form-item>

            </el-col>

            <el-col :span="8">
              <!-- 商品库存 -->
              <el-form-item label="商品库存" prop="num">
                <el-input v-model.number="editForm.num"></el-input>
              </el-form-item>

            </el-col>
            <el-col :span="8">
              <el-form-item label="分类id" prop="categoryId">
                <el-input v-model.number="editForm.categoryId"></el-input>
              </el-form-item>
            </el-col>

          </el-row>

          <!-- 商品状态和商品优先级 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="商品状态" prop="status">
                <el-select v-model="editForm.status" placeholder="请选择" style="width: 200px">
                  <el-option label="上架" value="1"></el-option>
                  <el-option label="下架" value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="优先级" prop="priority">
                <el-input v-model.number="editForm.priority"></el-input>
              </el-form-item>
            </el-col>


          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="图片URL" prop="price">
                <el-input v-model.number="editForm.price"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="IsEdit = false">取 消</el-button>
          <el-button type="primary" @click="editMsg">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import request from "@/api/request";
//   import Cookies from "js-cookie";

export default {
  name: "StudentList",
  data() {
    return {
      addForm: {
        itemType: '',
        title: '',
        sellPoint: '',
        price: null,
        num: null,
        image: '',
        status: null,
        priority: null,
        categoryId: null,
      },
      editForm: {
        itemType: '',
        title: '',
        sellPoint: '',
        price: null,
        num: null,
        image: '',
        status: '',
        priority: null,
        categoryId: null,
      },


      IsAdd: false,
      IsEdit: false,
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {},
      params: {
        pageSize: 10,
        pageIndex: 1,
        title: '',
        sellPoint: ''
        // categoryId: "",
      },

    };
  },
  created() {
    this.load();
  },
  filters: {
    formatTime: function (value) {
      if (!value) return '';
      const date = new Date(value);
      return date.toLocaleString(); // 或者使用其他合适的时间格式化方法
    }
  },
  methods: {
    addMsg() {
      this.$refs["addform"].validate((valid) => {
        if (valid) {
          request.post("/api/product/add", this.addForm).then((res) => {
            if (res.code === 20000) {
              this.$notify.success("新增成功");
              this.IsAdd = false;
              this.load();
            } else {
              this.$notify.error(res.msg);
            }
          });
        }
      });
    },
    editMsg() {
      this.$refs["editform"].validate((valid) => {
        if (valid) {
          request.put("/api/product/update", this.editForm).then((res) => {
            if (res.code === 20000) {
              this.$notify.success("更新成功");
              this.$refs["editform"].resetFields();
              this.IsEdit = false;
              this.load();
            } else {
              this.$notify.error(res.msg);
            }
          });
        }
      });
    },
    edit(row) {
      this.IsEdit = true;
      request.get("/api/product/" + row.id).then((res) => {
        console.log(res.data);
        this.editForm = res.data;
      });
    },
    load() {
      request
          .post("/api/product/listByPageHelper", this.params)
          .then((res) => {
            if (res.code === 20000) {
              this.tableData = res.data.list;
              this.total = res.data.total;
            }
          });
    },
    reset() {
      this.params = {
        pageSize: 10,
        pageIndex: 1,
      };
      this.load();
    },
    // 换页
    handleCurrentChange(pageNum) {
      this.params.pageIndex = pageNum;
      this.load();
    },
    add() {
      // 清空表单

      this.IsAdd = true;
      this.$refs["addform"].resetFields();
    },
    del(productId) {
      request.delete("/api/product/delete?productId=" + productId).then((res) => {
        if (res.code === 20000) {
          this.$notify.success("删除成功");
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
.el-form-item__label {
  align-items: flex-start;
  box-sizing: border-box;
  color: var(--el-text-color-regular);
  display: inline-flex;
  flex: 0 0 auto;
  font-size: var(--el-form-label-font-size);
  height: 32px;
  justify-content: flex-end;
  line-height: 32px;
  padding: 0 12px 0 0;
  font-weight: bold;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
